﻿using System.Web;
using Castle.Facilities.FactorySupport;
using OTE.Web.Infrastructure.Session;
using OTE.Web.Mvc.ViewModels.ViewModelMapper;

namespace OTE.Web.Mvc
{
    using System.Web.Mvc;
    using Castle.MicroKernel.Registration;
    using Castle.Windsor;
    using CommonServiceLocator.WindsorAdapter;
    using Controllers;
    using Infrastructure;
    using Infrastructure.CastleWindsor;
    using Microsoft.Practices.ServiceLocation;
    using SharpArch.Web.Mvc.Castle;

    public partial class MvcApplication : GlobalRegistration
    {
        protected override void InitializeServiceLocator()
        {
            IWindsorContainer container = new WindsorContainer();

            ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(container));

            container.RegisterControllers(typeof(HomeController).Assembly);

            container.AddFacility<FactorySupportFacility>();

            //container.Register(Component.For<HttpContextBase>().LifeStyle.Transient);
            //.UsingFactoryMethod(() => new HttpContextWrapper(HttpContext.Current)));

            container.Register(Component.For<HttpRequestBase>()
              .LifeStyle.PerWebRequest
              .UsingFactoryMethod(() => new HttpRequestWrapper(HttpContext.Current.Request)));

            container.Register(Component.For<HttpContextBase>()
                .LifeStyle.PerWebRequest
                .UsingFactoryMethod(() => new HttpContextWrapper(HttpContext.Current)));

            container.Register(Component.For<IPersistentWebState>().LifeStyle.PerWebRequest.ImplementedBy<WebSessionState>());

            ComponentRegistrar.AddComponentsTo(container);

            ServiceLocator.SetLocatorProvider(() => new WindsorServiceLocator(container));

            AutoMapperConfiguration.Configure();
        }
    }
}